{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "fb7368eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8064a525",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             Name Hire Date   Salary  Leaves Remaining \n",
      "0       John Idle  08/15/14  50000.0                 10\n",
      "1   Smith Gilliam  04/07/15  65000.0                  6\n",
      "2  Parker Chapman  02/21/14  45000.0                  7\n",
      "3     Jones Palin  10/14/13  70000.0                  3\n",
      "4   Terry Gilliam  07/22/14  48000.0                  9\n",
      "5   Michael Palin  06/28/13  66000.0                  8\n"
     ]
    }
   ],
   "source": [
    "df = pd.read_csv('./test_csv.csv')\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c00ac890",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "5e965c76",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     Name  Age      City  Salary\n",
      "ID                              \n",
      "1    Jack   28   Beijing   22000\n",
      "2    Lida   32  Shanghai   19000\n",
      "3    John   43  Shenzhen   12000\n",
      "4   Helen   38  Hengshui    3500\n",
      "Name      object\n",
      "Age        int64\n",
      "City      object\n",
      "Salary     int64\n",
      "dtype: object\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Age</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Jack</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Lida</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>John</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Helen</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     Name  Age\n",
       "ID            \n",
       "1    Jack   28\n",
       "2    Lida   32\n",
       "3    John   43\n",
       "4   Helen   38"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2=pd.read_csv('./read_csv.csv', sep=',', index_col=['ID'])\n",
    "print(df2)\n",
    "print(df2.dtypes)\n",
    "df2[['Name','Age']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "c3d25f21",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     Name  Age\n",
      "ID            \n",
      "1    Jack   28\n",
      "2    Lida   32\n",
      "3    John   43\n",
      "4   Helen   38\n",
      "Name    object\n",
      "Age      int64\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "df2=pd.read_csv('./read_csv.csv', sep=',', index_col=['ID'],dtype={'Salary':np.float64,'City':np.str_}, usecols=[0,1,2])\n",
    "print(df2)\n",
    "print(df2.dtypes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b47cb387",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   1   Jack  28   Beijing  22000\n",
      "0  2   Lida  32  Shanghai  19000\n",
      "1  3   John  43  Shenzhen  12000\n",
      "2  4  Helen  38  Hengshui   3500\n"
     ]
    }
   ],
   "source": [
    "df2=pd.read_csv('./read_csv.csv', sep=',',dtype={'Salary':np.float64,'City':np.str_}, skiprows=1)\n",
    "print(df2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "98ba5320",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   a      b   c         d      e\n",
      "0  1   Jack  28   Beijing  22000\n",
      "1  2   Lida  32  Shanghai  19000\n",
      "2  3   John  43  Shenzhen  12000\n",
      "3  4  Helen  38  Hengshui   3500\n"
     ]
    }
   ],
   "source": [
    "df2=pd.read_csv('./read_csv.csv', sep=',',dtype={'Salary':np.float64,'City':np.str_},names=['a','b','c','d','e'], header=0)\n",
    "print(df2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5796d39",
   "metadata": {},
   "source": [
    "# to_csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "61c081a8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DataFrame:\n",
      "      Name   ID    Language\n",
      "0   Smith  101      Python\n",
      "1  Parker  102  JavaScript\n",
      "csv_data:\n",
      " ,Name,ID,Language\r\n",
      "0,Smith,101,Python\r\n",
      "1,Parker,102,JavaScript\r\n",
      "\n"
     ]
    }
   ],
   "source": [
    "data = {'Name': ['Smith', 'Parker'], 'ID': [101, 102], 'Language': ['Python', 'JavaScript']} \n",
    "info = pd.DataFrame(data) \n",
    "csv_data = info.to_csv()\n",
    "print('DataFrame:\\n', info)\n",
    "print('csv_data:\\n', csv_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "00b0dc92",
   "metadata": {},
   "outputs": [],
   "source": [
    "csv_data=info.to_csv('./write_csv.csv', sep='|')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54c643d9",
   "metadata": {},
   "source": [
    "# excel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "06faf666",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输出成功\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "#创建DataFrame数据\n",
    "info_website = pd.DataFrame({'name': ['编程帮', 'c语言中文网', '微学苑', '92python'],\n",
    "     'rank': [1, 2, 3, 4],\n",
    "     'language': ['PHP', 'C', 'PHP','Python' ],\n",
    "     'url': ['www.bianchneg.com', 'c.bianchneg.net', 'www.weixueyuan.com','www.92python.com' ]})\n",
    "#创建ExcelWrite对象\n",
    "writer = pd.ExcelWriter('website.xlsx')\n",
    "info_website.to_excel(writer)\n",
    "writer.save()\n",
    "print('输出成功')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "82fc556a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'E:\\\\python\\\\pandas数据分析基础'"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "os.path.abspath('./')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "21d03580",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          Unnamed: 0  rank language                 url\n",
      "name                                                   \n",
      "微学苑                2     3      PHP  www.weixueyuan.com\n",
      "92python           3     4   Python    www.92python.com\n",
      "          col_label  rank language                 url\n",
      "name                                                  \n",
      "微学苑               2     3      PHP  www.weixueyuan.com\n",
      "92python          3     4   Python    www.92python.com\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_81972\\4177146493.py:4: FutureWarning: The default value of regex will change from True to False in a future version.\n",
      "  df.columns = df.columns.str.replace('Unnamed.*', 'col_label')\n"
     ]
    }
   ],
   "source": [
    "df = pd.read_excel('website.xlsx', index_col='name', skiprows=[1,2])\n",
    "print(df)\n",
    "#处理未命名列\n",
    "df.columns = df.columns.str.replace('Unnamed.*', 'col_label')\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95ea006e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
